#include "Task.h"
#include "Debug.h"
#include "Syscall.h"
#include "LibSyscall.h"

int FirstTaskMain(void)
{
    int count = 3;

    int pid = GetProcessId();
    PrintMsg("first task id=%d", pid);
    
    pid = Fork();
    if (pid < 0) {
        PrintMsg("create child proc failed.", 0);
    } else if (pid == 0) {
        PrintMsg("child: %d", count);

        char* argv[] = {"arg0", "arg1", "arg2", "arg3"};
        Execve("/shell.elf", argv, (char**)0);
    } else {
        PrintMsg("child task id=%d", pid);
        PrintMsg("parent: %d", count);
    }

    pid = GetProcessId();
    for(;;)
    {
        PrintMsg("task id = %d", pid);
        // 用户代码（CPL=3）运行时，如果 DS/ES/FS/GS 还指向 DPL=0 的内核段，用户代码访问数据时，CPL=3 > DPL=0，不满足访问规则，CPU会触发 General Protection Fault。
        // SysSleep(1000);
        // Static inline method
        MSleep(1000);
    }
    return 0;
}
